// 类型收缩
const isString = (str: any) => typeof str === 'string'
//不能判断object，Array，function，null

const isArr = (arr: any) => arr instanceof Array

// 2.类型谓词 |自定义守卫
// 实现一个函数，该函数可以传入任何类型
// 但是如果是object就检查里面的属性，
// 如果里面的属性是number就取两位
// 如果是string就去除左右空格
// 如果是函数就执行
// const isObj= (arg:any) => Object.prototype.toString.call(arg) === '[object Object]' 
const isObj = (arg: any): arg is Object => ({}).toString.call(arg) === '[object Object]'
const isFn = (arg: any): arg is Function => arg instanceof Function
const isNum = (arg: any): arg is number => typeof arg === 'number'

const isStr = (arg: any): arg is string => typeof arg === 'string'
const func = (arg:any) => {
    if (isObj(arg)) {
        Object.keys(arg).forEach(key => {
            const value = arg[key]
            if (isNum(value)) {
                arg[key] = value.toFixed(2)
            }
            if (isStr(value)) {
                arg[key] = value.trim()
            }
            if (isFn(value)) {
                // value()  //不能这样，会改变函数内部的this  global
                arg[key]()     //oo
            }
        })
    }


}

const oo = {
    a:12.3456,
    b:'    asdf   ',
    c:function(){
        console.log('this:',this)
    }
}
func(oo)
console.log(oo)